package com.hackvan2012.api;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class YellowAPIService {
	
	public static final String SANDBOX_API_KEY = "4xt2mq6vfmfwguub9nyz282n";
	
	public static final String PRODUCTION_API_KEY = "vvt6n9d6nzwb3cpm85kyz76f";
	
	public static final String SANDBOX_API_URL = "http://api.sandbox.yellowapi.com";
	
	public static final String PRODUCTION_API_URL = "http://api.yellowapi.com";
	
	private static String UID = "bobsyouruncle";
	
	/**
	 * Generates the url for a FindBusiness API call
	 * @param what
	 * @param where
	 * @param sandbox True if we are in the sandbox environment
	 * @return The url to load the results
	 */
	public static String findBusinessURL(String what, String where, boolean sandbox) {
		String url = "";
		if(sandbox) {
			url += SANDBOX_API_KEY + "/FindBusiness/?apikey=" + SANDBOX_API_URL;
		} else {
			url += PRODUCTION_API_KEY + "/FindBusiness/?apikey=" + PRODUCTION_API_URL;
		}
		url += "&what=" + what + "&where=" + where + "&lang=en&UID=" + UID;
		return url;
	}
	
	public static String findBusinessDetailsURL(String province, String city, String businessName, String listingId, boolean sandbox) {
		String url = "";
		if(sandbox) {
			url += SANDBOX_API_KEY + "/GetBusinessDetails/?apikey=" + SANDBOX_API_URL;
		} else {
			url += PRODUCTION_API_KEY + "/GetBusinessDetails/?apikey=" + PRODUCTION_API_URL;
		}
		//TODO city is optional
		url += "&prov=" + province + "&city=" + city + "&bus-name=" + businessName + "&listingId=" + listingId + "&lang=en&UID=" + UID;
		return url;
	}
	
	/**
	 * Generates the url for a FindDealer API call
	 * @param parentID
	 * @param sandbox True if we are in the sandbox environment
	 * @return The url to load the results
	 */
	public static String findDealerURL(String parentID, boolean sandbox) {
		String url = "";
		if(sandbox) {
			url += SANDBOX_API_KEY + "/FindDealer/?apikey=" + SANDBOX_API_URL;
		} else {
			url += PRODUCTION_API_KEY + "/FindDealer/?apikey=" + PRODUCTION_API_URL;
		}
		url += "&pid=" + parentID + "&lang=en&UID=" + UID;
		return url;
	}
	
	/**
	 * Generates the url for a GetTypeAhead API call
	 * @param what True if what, false if where
	 * @param sandbox True if we are in the sandbox environment
	 * @return The url to load the results
	 */
	public static String getTypeAheadURL(String text, boolean what, boolean sandbox) {
		String url = "";
		if(sandbox) {
			url += SANDBOX_API_KEY + "/FindBusiness/?apikey=" + SANDBOX_API_URL;
		} else {
			url += PRODUCTION_API_KEY + "/FindBusiness/?apikey=" + PRODUCTION_API_URL;
		}
		if(what) {
			url += "&field=WHAT";
		} else {
			url += "&field=WHERE";
		}
		url += "&text=" + text + "&lang=en&UID=" + UID;
		return url;
	}
	
	private Map<String,String> apiParameters = new HashMap<String,String>();
	
	private boolean sandbox;
	
	public YellowAPIService( Map<String,String[]> params, boolean sandbox){
		Set<String> keySet = params.keySet();
		for(String k : keySet)
			apiParameters.put(k, params.get(k)[0]);
		this.sandbox = sandbox;
		if(sandbox)
			apiParameters.put("apikey", SANDBOX_API_KEY);
		else
			apiParameters.put("apikey", PRODUCTION_API_KEY);
		
	}
	
	public YellowAPIService(String what, String where, boolean sandbox) {
		this.sandbox = sandbox;
		if(sandbox)
			apiParameters.put("apikey", SANDBOX_API_KEY);
		else
			apiParameters.put("apikey", PRODUCTION_API_KEY);
		
		apiParameters.put("what", what);
		apiParameters.put("where", where);
		apiParameters.put("method", "findbusiness");
		apiParameters.put("UID", "bobsyouruncle");
	}
	
	public String call(){
		
		String call = "";
		if(sandbox)
			call += SANDBOX_API_URL;
		else
			call += PRODUCTION_API_URL;
		
		String method = apiParameters.get("method");
		if(method.equalsIgnoreCase("findbusiness")) {
			call += "/FindBusiness/?";
		}
		else if(method.equalsIgnoreCase("getbusinessdetails")) {
			call += "/GetBusinessDetails/?";
		}
		else if(method.equalsIgnoreCase("finddealer")) {
			call += "/FindDealer/?";
		}
		else if(method.equalsIgnoreCase("gettypeahead")) {
			call += "/GetTypeAhead/?";
		}
		Set<String> keySet = apiParameters.keySet();
		for(String s : keySet){
			if(!s.equals("sandbox") || !s.equals("method"))
				call += s+"="+apiParameters.get(s)+"&";
		}
		String xmlout = "";
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new URL(call).openStream()));
			String line = br.readLine();
			xmlout += line+"\n";
			while(line != null){
				xmlout += br.readLine()+"\n";
				line = br.readLine();
			}
			
		} catch (Exception e){
		}
		return xmlout;
	}

}
